package com.samsung.spen.sshare.networking.networkservice;

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.os.AsyncTask;
import android.util.Log;

import com.samsung.spen.sshare.networking.sendreceivefile.SubFileExtend;

/* 
 *  param[0] : IP
 *  param[1] : port
 *  param[2] : service Type
 *  param[3...n]: data
 */
public class SendingDataTask extends AsyncTask<String, Integer, Void> {
	SubFileExtend fileExtends=null;
	private static final String TAG = "SENDINGDATATASK";
	
	public SendingDataTask() {
	}
	
	public SendingDataTask(SubFileExtend fileExtends) {
		this.fileExtends=fileExtends;
	}
	
	@Override
	protected Void doInBackground(String... params) {
		Socket socket = null;
		try {
//			socket = new Socket(params[0], Integer.parseInt(params[1]));
			socket = new Socket();
			Log.i(TAG, "vao truyen du lieu");
			socket.connect(
					new InetSocketAddress(params[0], Integer.parseInt(params[1])), 5000);
			Log.i(TAG, "ket noi server xong");	
			
			DataOutputStream os = new DataOutputStream(
					new BufferedOutputStream(socket.getOutputStream()));
//			Log.d(ServerSocketService.tag, "Sending Service: " + params[2]);
			/*
			 * Send service type
			 */
//			os.writeUTF(params[2]);
//			os.flush();
			
			//Check service type
			if (params[2].equals(SendReceiveService.CONNECT)) {
				/*
				 * Send port number to server
				 */
//				Log.d(ServerSocketService.tag, "Port Service: " + params[3]);
				os.writeUTF(params[2]);
				os.flush();
				os.writeInt(Integer.parseInt(params[3]));
				os.flush();
				
			} else if (params[2].equals(SendReceiveService.DISCONNECT)) {
				os.writeUTF(params[2]);
				os.flush();
			} else if (params[2].equals(SendReceiveService.INDEX)) {
				// Mr Dohai 's task
				os.writeUTF(SendReceiveService.INDEX+"%"+params[3]);
				os.flush();
			} else if (params[2].equals(SendReceiveService.DATALIST)) {
				// Mr Dohai 's task
				os.writeUTF(SendReceiveService.DATALIST+"%"+params[3]);
				os.flush();
			} else if (params[2].equals(SendReceiveService.DATA)) {
				// Mr Dohai 's task
				
				Log.i(TAG+"SEND",params[3]);
				File file=new File(params[3]);
				
				String fileName=file.getName();
				os.writeUTF(params[2]+"%"+fileName+"%"+fileExtends.getIndex());
				os.flush();
				
				FileInputStream fis=new FileInputStream(file);
				
				int n=0;
				long lengthOfFile=0;
				byte[] buf=new byte[4096];
				while((n=fis.read(buf))!=-1){
					os.write(buf,0,n);
					os.flush();
					lengthOfFile+=n;
					if(fileExtends!=null){
						Log.e("update percent", lengthOfFile+"");
						fileExtends.percent=(int)lengthOfFile%100;
					}
				}
				fis.close();
				os.close();
				Log.i(TAG, "da truyen xong");
			}
			
		} catch (UnknownHostException e) {
			// e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO: handle exception
			e.printStackTrace();
			Log.i(TAG, "File not found");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if (socket != null) {
				if (socket.isConnected()) {
					try {
						socket.close();
						Log.i(TAG, "Socket da dong");
						socket = null;
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

			}
		}

		return null;
	}
}
